Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: Add publishing to PyPI and TestPyPI with trusted publishers #51

Merged

Conversation

matthewfeickert
Copy link
Contributor

@matthewfeickert matthewfeickert commented Jul 16, 2023

Resolves #50

Use the OpenID Connect (OIDC) standard to publish to PyPI and TestPyPI using PyPI's "Trusted Publisher" implementation to publish without using API tokens stored as GitHub Actions secrets. Use an optional GitHub Actions environment to further restrict publishing to selected branches for additional security.

@matthewfeickert
Copy link
Contributor Author

@asmeurer this is mostly ready to go, but before this gets finished I'll need you to add a trusted publisher to TestPyPI and PyPI with repo name ata-apis/array-api-compat and workflow name of .github/workflows/publish-package.yml.

https://docs.pypi.org/trusted-publishers/adding-a-publisher/

Comment on lines 4 to 7
# branches:
# - main
# tags:
# - v*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just commented out for me to verify things worked on my fork. I'll finish this soon and clean up and add context in PR body.

@asmeurer
Copy link
Member

@asmeurer this is mostly ready to go, but before this gets finished I'll need you to add a trusted publisher to TestPyPI and PyPI with repo name ata-apis/array-api-compat and workflow name of .github/workflows/publish-package.yml.

I've done this. Should I add something for the environment name?

@matthewfeickert
Copy link
Contributor Author

I've done this. Should I add something for the environment name?

Ah yeah, sorry I forgot to mention this. @asmeurer can you go into https://github.com/data-apis/array-api-compat/settings/environments and add an environment named publish-package with deployment branches that match to pasterns of main, v*, and whatever release branch names backports might be released against (e.g. release/*)? c.f. scikit-hep/pyhf#2183 for lots of discussion on this. Let me know if you have any questions.

Screenshot from 2023-07-18 00-34-44

@matthewfeickert matthewfeickert force-pushed the ci/add-trusted-publishers-workflow branch from 54d49b7 to 10e7d95 Compare July 18, 2023 05:58
@matthewfeickert matthewfeickert marked this pull request as ready for review July 18, 2023 05:58
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependabot config is only for updating GitHub Actions, and so the number of PRs generated should be substantially less than if Python packages were being considered.

- "github-actions"
- "dependencies"
reviewers:
- "asmeurer"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes @asmeurer wants to be the person to get the ping.

pull_request:
branches:
- main
- release/v*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a assuming a release branch pattern for future backports for bug fixes to patch versions of release/v* which may not be the case. This can be altered as desired.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to worry about backports here. I'm trying to think of a situation where we'd want to do that.

We generally try to avoid making backwards incompatible changes to the standard itself, but even if there was one, I think we would want to support multiple versions in the same array-api-compat version using api_version. Similarly, we'll probably want to support both NumPy 2.0 and NumPy 1.25 at the same time when that happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to worry about backports here.

Sounds good. I'll drop this then if you're not planning on making releases from any branch other than main.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has now been removed.

Comment on lines +12 to +13
release:
types: [published]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit is for publishing on GitHub Release.

Comment on lines +14 to +21
workflow_dispatch:
inputs:
publish:
type: choice
description: 'Publish to TestPyPI?'
options:
- false
- true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would allow you to create a release to TestPyPI from workflow dispatch. If this isn't of interest this can get removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is worth doing at least the first time around. How do you do it? Is there some option when you create the release in the GitHub interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do this through workflow dispatch by visiting the GitHub Actions tab for the repo and then selecting this workflow (https://github.com/data-apis/array-api-compat/actions/workflows/publish-package.yml) and then when you click the "Run workflow" button on the right hand side there will be the option to select false or true from a dropdown.

This is how it looks like for us on pyhf's GitHub repo:

pyhf-view

and if it helps, starting around 52 seconds into this video of me making the pyhf v0.7.1 release I show this button example: https://youtu.be/ZV20tr3EpTw?t=52


- name: Install python-build and twine
run: |
python -m pip install --upgrade pip setuptools
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If array-api-compat ever moves off setuptools this setuptools bit here could be removed.

Comment on lines 90 to 99
- name: Publish distribution 📦 to Test PyPI
# Publish to TestPyPI on tag events of if manually triggered
# Compare to 'true' string as booleans get turned into strings in the console
if: >-
(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v'))
|| (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true')
uses: pypa/[email protected]
with:
repository-url: https://test.pypi.org/legacy/
print-hash: true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Publishing to TestPyPI (which is optional of course) would require following these instructions as there is no array-api-compat yet on TestPyPI: https://docs.pypi.org/trusted-publishers/creating-a-project-through-oidc/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right link? I don't see testpypi mentioned on that page.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the right link. Both PyPI and TestPyPI have the setup for trusted publishing in the same area. So just treat all instances of PyPI as TestPyPI for creating an project with a Trusted Publishers workflow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bumped this from pypa/[email protected] to pypa/[email protected].

print-hash: true

- name: Publish distribution 📦 to PyPI
if: github.event_name == 'release' && github.event.action == 'published'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is assuming that you only want to publish to PyPI via creating a GitHub Release.

@matthewfeickert matthewfeickert force-pushed the ci/add-trusted-publishers-workflow branch 2 times, most recently from 0502792 to 32f9ec1 Compare August 1, 2023 19:19
@matthewfeickert
Copy link
Contributor Author

Sorry for the 2 week delay — I was traveling for work and then finishing faculty applications — but this should be ready for review again.

@asmeurer
Copy link
Member

asmeurer commented Aug 1, 2023

Thanks for the update. It might be another 2 weeks before I myself am able to look at this.

@matthewfeickert
Copy link
Contributor Author

No worries! Just ping whenever you have questions.

* Use the OpenID Connect (OIDC) standard to publish to PyPI and TestPyPI
  using PyPI's "Trusted Publisher" implementation to publish without
  using API tokens stored as GitHub Actions secrets. Use an optional
  GitHub Actions environment to further restrict publishing to selected
  branches for additional security.
   - c.f. https://blog.pypi.org/posts/2023-04-20-introducing-trusted-publishers/
   - c.f. https://docs.pypi.org/trusted-publishers/
* Add Dependabot config for weekly updates to GitHub Actions for
  security best practices.
   - c.f. https://learn.scientific-python.org/development/guides/gha-basic/#updating
@matthewfeickert matthewfeickert force-pushed the ci/add-trusted-publishers-workflow branch from 32f9ec1 to 1f146b6 Compare August 18, 2023 18:22
@asmeurer
Copy link
Member

We need to do a release so I'm going to merge this and see if it works.

@asmeurer asmeurer merged commit 31aec62 into data-apis:main Sep 11, 2023
@matthewfeickert matthewfeickert deleted the ci/add-trusted-publishers-workflow branch September 12, 2023 01:26
@matthewfeickert
Copy link
Contributor Author

We need to do a release so I'm going to merge this and see if it works.

@asmeurer let me know how this goes. Tag based deployments under environments have started breaking recently given https://github.com/orgs/community/discussions/62991, so for the most recent release I've made (scikit-hep/pyhf#2319) I had to temorarily set the deployment branches in the "publish-package" environment to "all branches", which invalidates #51 (comment) a bit.

@matthewfeickert matthewfeickert mentioned this pull request Sep 12, 2023
# Publish to TestPyPI on tag events of if manually triggered
# Compare to 'true' string as booleans get turned into strings in the console
if: >-
(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asmeurer Also catching now that as you use tags that match

    - [0-9]+.[0-9]+
    - [0-9]+.[0-9]+.[0-9]+

but not v* that these will never publish to TestPyPI on tag. I should fix this in a follow up PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Interest in publishing workflows?
2 participants